package com.wze.wze.Controller;

import com.wze.wze.Aop.PasToken;
import com.wze.wze.Entity.Music;
import com.wze.wze.Entity.PingLun;
import com.wze.wze.Entity.ResultEntity;
import com.wze.wze.Entity.UserTab;
import com.wze.wze.Service.PingLunService;
import com.wze.wze.Util.JSONTool;
import com.wze.wze.Util.OSSUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域
@RestController //控制器注解
@RequestMapping("/PingLun/")//请求路径
public class PingLunController {
    @Resource
    PingLunService pingLunService;
    ResultEntity resultEntity = null;

    @GetMapping("PingLun")
    private String getPingLunByuid(@RequestParam("uid") int uid,@RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            PingLun pingLun = pingLunService.getPingLunByid(uid,mid);
            if (pingLun == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此音乐");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(pingLun);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员1");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);

    }


    @DeleteMapping("PingLunByid")
    private String delMusic(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            if (pingLunService.delPingLunById(id)> 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员2");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @DeleteMapping("PingLun")
    private String delMusic(@RequestParam("uid") int uid,@RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            if (pingLunService.delPingLunByuId(uid, mid)> 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员3");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("addPingLun")
    private String addPl( @RequestParam("uid") int uid,@RequestParam("UName") String UName,@RequestParam("Info") String Info, @RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            if (pingLunService.addPingLun(uid, UName, Info, mid)>0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员4");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PutMapping("PingLun")
    private String upUser(@RequestParam("Info") String Info, @RequestParam("id") int id) {
        resultEntity = new ResultEntity();
            try {
                if (pingLunService.upPingLun(Info, id) > 0) {
                    resultEntity.setCode("0");
                    resultEntity.setMsg("修改成功！");
                } else {
                    resultEntity.setCode("1");
                    resultEntity.setMsg("修改失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultEntity.setCode("9999");
                resultEntity.setMsg("系统错误，请联系管理员5");
            }


        return JSONTool.getJsonTool().toJson(resultEntity);
    }


    @PasToken
    @GetMapping("pinglunList")
    private String PlList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("mid")  int mid,
            @RequestParam("uid") int uid
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<PingLun> list = pingLunService.getPingLunList(Start, limit, uid, mid);
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败6!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }


}
